package com.lorenzogao.customview.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lorenzogao.customviewexample.R
import kotlinx.android.synthetic.main.main_item.view.*

/**
 *  create by LorenzoGao
 *  email: 2508719070@qq.com
 *  TIME: 2019-11-13 16:46
 *  description:
 */

class MainAdapter(context: Context, data: MutableList<String>) : RecyclerView.Adapter<MainAdapter.MainViewHoulder>() {


    private val mContext = context

    private val mData = data


     var listener: OnItemClickListener? = null


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHoulder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.main_item, parent, false)
        return MainViewHoulder(view)
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    override fun onBindViewHolder(holder: MainViewHoulder, position: Int) {

        holder.itemView.tv_main_item.text = mData[position]

        holder.itemView.setOnClickListener {
            listener?.onItemClick(position)
        }


    }


    class MainViewHoulder(itemlayout: View) : RecyclerView.ViewHolder(itemlayout)


    interface OnItemClickListener {

        fun onItemClick(position: Int)

    }

}